package com.eastfair.certificate.controller;

import com.eastfair.certificate.dto.CertificateAuthDTO;
import com.eastfair.certificate.entity.Certificate;
import com.eastfair.certificate.entity.CertificateAuth;
import com.eastfair.certificate.dto.CertificateAuthSaveDTO;
import com.eastfair.certificate.dto.CertificateAuthUpdateDTO;
import com.eastfair.certificate.dto.CertificateAuthPageQuery;
import com.eastfair.certificate.service.CertificateAuthService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.service.CertificateService;
import com.eastfair.certificate.vo.CertificateAuthVO;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 证件审核表
 * </p>
 *
 * @author ligang
 * @date 2022-07-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/certificateAuth")
@Api(value = "CertificateAuth", tags = "证件审核表")
@PreAuth(replace = "certificate:certificateAuth:")
public class CertificateAuthController extends SuperController<CertificateAuthService, Long, CertificateAuth, CertificateAuthPageQuery, CertificateAuthSaveDTO, CertificateAuthUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    CertificateService certificateService;

    @Override
    public void handlerResult(IPage<CertificateAuth> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<CertificateAuth> certificateAuthList = list.stream().map((map) -> {
            CertificateAuth certificateAuth = CertificateAuth.builder().build();
            //TODO 请在这里完成转换
            return certificateAuth;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(certificateAuthList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CertificateAuthPageQuery> params) {
        CertificateAuthPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 批量查询
     *
     * @param certificateAuthDTO 批量查询
     * @return 查询结果
     */
    @ApiOperation(value = "批量查询", notes = "批量查询")
    @PostMapping("/queryVO")
    public R<List<CertificateAuthVO>> queryVO(@RequestBody CertificateAuthDTO certificateAuthDTO) {
        List<CertificateAuthVO> certificateAuthVOList = baseService.doToVos(baseService.list(null, null));
        Certificate certificate = certificateService.getByIdCache(certificateAuthDTO.getCertificateId());
        if (certificate != null) {
            for (CertificateAuthVO certificateAuthVO : certificateAuthVOList) {
                certificateAuthVO.setIsCheck(false);
                if (certificateAuthVO.getId().equals(certificate.getFirstAuthId()) || certificateAuthVO.getId().equals(certificate.getSecondAuthId())) {
                    certificateAuthVO.setIsCheck(true);
                }
                if (certificateAuthVO.getId().equals(certificate.getFirstAuthId())) {
                    certificateAuthVO.setUpdateTime(certificate.getFirstAuthTime());
                } else if (certificateAuthVO.getId().equals(certificate.getSecondAuthId())) {
                    certificateAuthVO.setUpdateTime(certificate.getSecondAuthTime());
                }

            }
        }
        return success(certificateAuthVOList);
    }
}
